package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.request.AnswerCreateParam;
import com.yc.cloud.admin.dto.request.AnswerListParam;
import com.yc.cloud.admin.service.AnswerService;
import com.yc.cloud.admin.service.QuestionService;
import com.yc.cloud.admin.vo.AnswerVo;
import com.yc.cloud.api.common.MetaHumanConstants;
import com.yc.cloud.basic.api.CommonPage;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.Answer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 答案库
 *
 * @author Yancy
 * @date 2024-01-31
 */
@RestController
@RequestMapping("/answer")
@Api(value = "答案库", tags = "答案库接口")
public class AnswerController {
    private final AnswerService answerService;
    private final QuestionService questionService;

    public AnswerController(AnswerService answerService, QuestionService questionService) {
        this.answerService = answerService;
        this.questionService = questionService;
    }


    @GetMapping
    @ApiOperation(value = "答案库列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<AnswerVo>> list(AnswerListParam param, BaseQueryParam queryParam) {
        val result = answerService.list(param, queryParam);
        return ResponseResult.success(CommonPage.restPage(result));
    }


    @GetMapping("{id}")
    @ApiOperation(value = "答案库详情", notes = "根据ID查询")
    public ResponseResult<AnswerVo> get(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = answerService.get(id);
        return ResponseResult.success(answerService.convertVo(result));
    }


    @PostMapping
    @ApiOperation(value = "添加答案库", notes = "添加,step相同则表示同时显示")
    public ResponseResult<Answer> create(@Validated @RequestBody AnswerCreateParam param) {
        if (questionService.getByQuestionNo(param.getQuestionNo()) == null) {
            return ResponseResult.failed(MetaHumanConstants.QUESTION_NO_NOT_EXIST);
        }
        val result = answerService.create(param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }


    @ApiOperation(value = "删除答案库", notes = "通过ID删除")
    @DeleteMapping(value = "{id}")
    public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = answerService.delete(id);
        return result ? ResponseResult.success(true) : ResponseResult.failed();
    }


    @ApiOperation(value = "修改答案库", notes = "通过ID修改")
    @PutMapping(value = "{id}")
    public ResponseResult<Answer> update(@PathVariable @ApiParam(value = "ID", required = true) Long id, @Validated @RequestBody Answer param) {
        val result = answerService.update(id, param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }
}
